package Api;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@WebServlet(urlPatterns="/search")
public class Controller extends HttpServlet{
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub

		String str = Api.getWebCon("http://localhost:9092/invoice");
		JSONArray json = JSONArray.fromObject(str);
		
		List<SettlementMain> list = new ArrayList<SettlementMain>();

		for (int i = 0; i < json.size(); i++) {
			
			JSONObject job = json.getJSONObject(i);
			
			SettlementMain settlementMain = new SettlementMain();
			
			settlementMain.setSettlementNo(job.get("settlementNo").toString());
			settlementMain.setInsert_date(job.get("insert_date").toString());
			
			String result = job.get("result").toString();
			if ("1".equals(result)) {
				settlementMain.setResult("上传成功");
				settlementMain.setJson(job.get("settlementUploadJson").toString());
			}if ("2".equals(result)) {
				settlementMain.setResult("上传失败");
				settlementMain.setProcess_information(job.get("process_information").toString());
				settlementMain.setJson(job.get("settlementUploadJson").toString());
			}if ("3".equals(result)) {
				settlementMain.setResult("开票成功");
				settlementMain.setJson(job.get("sellerInvoicePushJson").toString());
			}
			
			list.add(settlementMain);
		}
		
		System.out.println(list);
		
		req.setAttribute("LossenList", list);
		req.getRequestDispatcher("/page/index.jsp").forward(req, resp);
    }
}